import Vue from 'vue'
import VueRouter from 'vue-router'
const Home = () => import('../views/Home.vue')
const About = () => import('../views/About.vue')
const Login = () => import('../views/Login.vue')
const Welcome = () => import('../views/Welcome.vue')
const Users = () => import('../views/user/Users.vue')
const Rights = () => import('../views/power/Rights.vue')
const Roles = () => import('../views/power/Roles.vue')
const Cate = () => import('../views/goods/Cate.vue')
const Params = () => import('../views//goods/Params.vue')
const Goods = () => import('../views//goods/Goods.vue')


Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: "/Login"
  },
  {
    path: '/Home',
    name: 'Home',
    component: Home,
    redirect: 'Welcome',
    children: [{
        path: '/Welcome',
        name: 'Welcome',
        component: Welcome,
      },
      {
        path: '/users',
        name: 'Users',
        component: Users
      },
      {
        path: '/rights',
        name: 'Rights',
        component: Rights
      },
      {
        path: '/roles',
        name: 'Roles',
        component: Roles
      },
      {
        path: '/categories',
        name: 'Cate',
        component: Cate
      },
      {
        path: '/params',
        name: 'Params',
        component: Params
      },
      {
        path: '/goods',
        name: 'Goods',
        component: Goods
      },
    ]
  },
  {
    path: '/Login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/About',
    name: 'About',
    component: About,
  },
]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  // 放行前往Login页面
  if (to.path == "/Login") next();
  // 存储token
  const tokenStr = window.sessionStorage.getItem("token");
  //防止未登录进入页面,from.name防止第一进入页面弹出登录
  if (!tokenStr && from.name) {
    Vue.prototype.$message.warning("请先登录页面");
    return next("/Login")
  }

  next();
})

export default router